local Msg = require("core.Msg")
local ExcelMapConf = require("excel.Map")
local Map = require("core.Map")
local WorldMap = require("excel.WorldMap")
local MapEx = require("core.MapEx")
local ObjHuman = require("core.ObjHuman")
local ExcelMonster = require("excel.Monster")
local Buffer = require("excel.Buffer")
local ExcelNpc = require("excel.Npc")
local SkillLearn = require("skill.Learn")

local configs = {
    [1] = require("excel.Item").item,
    [2] = require("excel.Equip").equip,
    [3] = require("excel.Gem").gem,
    [4] = require("excel.Npc").npc,
    [5] = Buffer.Buff,
    [6] = require("excel.Shenbing").shenbing,
    [8] = require("excel.Skill").Skill,
}

local protoBuf = {}
local mapBuf = {}

local monsterShowOnMap = {}	-- key: mapID    value:monster conf
local npcShowOnMap = {}		-- key:mapID	 value:npc conf

function initMap()
    --monster
    for monsterID,monsterConf in pairs(ExcelMonster.monster) do
        if monsterConf.mapID ~= 0 and monsterConf.mapX ~= 0 and monsterConf.mapY ~= 0 and monsterConf.hide == 0 then
            if not monsterShowOnMap[monsterConf.mapID] then
                monsterShowOnMap[monsterConf.mapID] = {}
            end
            monsterShowOnMap[monsterConf.mapID][monsterID] = monsterConf
        end
    end

    --npc
    for npcID, npcConf in pairs(ExcelNpc.npc) do
        if npcConf.mapID ~= 0 and npcConf.hide == 0 then
            local nMapX = npcConf.mapX 
            local nMapY = npcConf.mapY 
            if not nMapX or not nMapY or nMapX <= 0 or nMapY <= 0 then
                local nMapExData = MapEx[npcConf.mapID]
                if nMapExData and nMapExData.npc and nMapExData.npc[0] then
                    for k,v in ipairs(nMapExData.npc[0]) do
                        if v[1] == npcID then
                            nMapX = v[2] 
                            nMapY = v[3] 
                        end
                    end
                end
            end
            if nMapX and nMapX > 0 and nMapY and nMapY > 0 then
                if not npcShowOnMap[npcConf.mapID] then
                    npcShowOnMap[npcConf.mapID] = {}
                end
                npcShowOnMap[npcConf.mapID][npcID] = npcConf
            end
        end
    end

    for k, conf in pairs(ExcelMapConf) do
        if type(k) == "number" and ExcelMapConf.scene[k] then
            mapBuf[k] = {
                [1] = Msg.proto_id.GC_MAP_CONF_QUERY,
                mapID = nil,
                mapWidth = nil,
                mapHeight = nil,
                door = {},
                npc = {},
                monster = {},
            }
            mapBuf[k].mapWidth,mapBuf[k].mapHeight = Map.getWidthHeight(k)
            local msg = mapBuf[k]
            local doors = msg.door
            doors[0] = 0
            for i = 1,8 do
                doors[i] = {}
            end

            local npcs = msg.npc
            npcs[0] = 0
            for i = 1,25 do
                npcs[i] = {}
            end

            local monsters = msg.monster
            monsters[0] = 0

            -- door
            for index, sceneEvent in pairs(conf) do
                if sceneEvent.action_cmd1 and sceneEvent.action_cmd1 == "jump" and ExcelMapConf.scene[sceneEvent.action_arg1[2]] then
                    local listRect = Map.getRects(k, sceneEvent.action_arg1[1])
                    local rect = listRect[1]
                    doors[0] = doors[0] + 1
                    local door = doors[doors[0]]
                    door.staticID = 0
                    door.name = ExcelMapConf.scene[sceneEvent.action_arg1[2]].name
                    door.ability_desc = ""
                    door.head_titleicon_id = 0
                    door.head_title_desc = ""
                    door.lv = 0
                    door.mapWellMinLv = ExcelMapConf.scene[sceneEvent.action_arg1[2]].well_minlv
                    door.mapWellMaxLv = ExcelMapConf.scene[sceneEvent.action_arg1[2]].well_maxlv
                    door.x = (rect[1] + rect[3]) / 2
                    door.y = (rect[2] + rect[4]) / 2
                    door.isPVP = ExcelMapConf.scene[sceneEvent.action_arg1[2]].isPVP
                    if MapEx[k].display then
                        for _, v in pairs(MapEx[k].display) do
                            for _, vv in pairs(v) do
                                if rect[1] <= vv[2] and vv[2] <= rect[3] and rect[2] <= vv[3] and vv[3] <= rect[4] then
                                    door.x = vv[2]
                                    door.y = vv[3]
                                    break
                                end
                            end
                        end
                    end
                end
            end

            --npc
            if npcShowOnMap[k] then
                for npcID,npcConf in pairs(npcShowOnMap[k]) do
                    npcs[0] = npcs[0] + 1
                    local npc =  npcs[npcs[0]]
                    if not npc then
                        npc = {}
                        npcs[npcs[0]] = npc
                    end
                    npc.staticID = npcID
                    npc.name = npcConf.name
                    npc.ability_desc = npcConf.ability_desc[1] or ""
                    npc.head_titleicon_id = npcConf.head_titleicon_id
                    npc.head_title_desc = npcConf.head_title_desc
                    npc.mapWellMinLv = 0
                    npc.mapWellMaxLv = 0
                    npc.lv = 0
                    npc.x = npcConf.mapX
                    npc.y = npcConf.mapY
                    npc.isPVP = 0
                end
            end

            --monster
            if monsterShowOnMap[k] then
                for monsterID,monsterConf in pairs(monsterShowOnMap[k]) do
                    monsters[0] = monsters[0] + 1
                    local monster =  monsters[monsters[0]]
                    if not monster then
                        monster = {}
                        monsters[monsters[0]] = monster
                    end
                    monster.staticID = monsterID
                    monster.name = monsterConf.name
                    monster.ability_desc = ""
                    monster.head_titleicon_id = 0
                    monster.head_title_desc = ""
                    monster.lv = monsterConf.lv
                    monster.mapWellMinLv = 0
                    monster.mapWellMaxLv = 0
                    monster.x = monsterConf.mapX
                    monster.y = monsterConf.mapY
                    monster.isPVP = 0
                end
            end

        end

    end

end

function initItem()
    for type,excels in pairs(configs) do
        protoBuf[type] = {}
        for id,excel in pairs(excels) do
            protoBuf[type][id] = {
                [1] = Msg.proto_id.GC_CONFIG_QUERY,
                type = type,
                id = id,
                configs = {}
            }
            local configs = protoBuf[type][id].configs

            for i = 1,10 do
                configs[i] = {}
            end

            if type == 1 then
                configs[0] = 2
                configs[1].value = excel.desc
                configs[2].value = excel.itemTypeName
            elseif type == 2 then
                configs[0] = 1
                configs[1].value = excel.desc
            elseif type == 3 then
                configs[0] = 2
                configs[1].value = excel.desc
                configs[2].value = excel.itemTypeName
            elseif type == 4 then
                configs[0] = 4
                configs[1].value = excel.speak
                configs[2].value = ""
                for index, ability in pairs(excel.ability) do
                    if configs[2].value == "" then
                        configs[2].value = configs[2].value .. ability
                    else
                        configs[2].value = configs[2].value .. "," .. ability
                    end
                end
                configs[3].value = ""
                for index, abilityDesc in pairs(excel.ability_desc) do
                    if configs[3].value == "" then
                        configs[3].value = configs[3].value .. abilityDesc
                    else
                        configs[3].value = configs[3].value .. "," .. abilityDesc
                    end
                end
                configs[4].value = ""
                for index, speak in pairs(excel.nearSpeak) do
                    if configs[4].value == "" then
                        configs[4].value = configs[4].value .. speak
                    else
                        configs[4].value = configs[4].value .. "|" .. speak
                    end
                end
            elseif type == 5 then
                configs[0] = 0
                local cmdConfig = Buffer.BuffCmd[excel.cmd]
                if cmdConfig then
                    local len = 1

                    for i=1,#cmdConfig.desc do
                        configs[i+1].value = tostring(cmdConfig.desc[i])
                        len = len + 1
                    end

                    configs[0] = len
                    configs[1].value = tostring(excel.describ)
                else
                    configs[0] = 1
                    configs[1].value = excel.describ
                end

            elseif type == 6 then --神兵绝技
                configs[0] = 1
                configs[1].value = excel.juejiDesc              
            elseif type == 8 then	-- 技能
                configs[0] = 4
                configs[1].value =  tostring(excel.icon)
                configs[2].value =  tostring(excel.name)
            end
        end
    end
end

initItem()
initMap()


function CG_CONFIG_QUERY(human,msg)
    local msgRet = protoBuf[msg.type][msg.id]
    if msgRet then
        if msg.type == 8 then	--技能特殊处理
            local skill = configs[8][msg.id]
            
            local level = SkillLearn.getLevel(human, msg.id) + ObjHuman.getSkillLevelUp(human, skill.group)
            msgRet.configs[3].value = tostring(skill.about[level] or skill.about[#skill.about])
            level = level + 1
            msgRet.configs[4].value = tostring(skill.about[level] or "已到顶级") 
        end
        Msg.send(msgRet, human.fd)
    end
end


function CG_MAP_CONF_QUERY(human,msg)
    local msgRet = mapBuf[msg.mapID]
    if not msgRet then
        return
    end
    msgRet.mapID = msg.mapID
    Msg.send(msgRet,human.fd)
end

function CG_WORLD_MAP_CONF_QUERY(human)
    local msgRet = Msg.gc.GC_WORLD_MAP_CONF_QUERY
    msgRet.datas1[0] = 0
    msgRet.datas2[0] = 0
    for _,v in ipairs(WorldMap.world) do

        local msgData
        if v.mserver == 0 then
            msgRet.datas1[0] = msgRet.datas1[0] + 1
            msgData = msgRet.datas1[msgRet.datas1[0]]
        else
            msgRet.datas2[0] = msgRet.datas2[0] + 1
            msgData = msgRet.datas2[msgRet.datas2[0]]
        end

        msgData.type = v.type
        msgData.mapID = v.mapID
        msgData.minLv = v.minLv
        msgData.maxLv = v.maxLv
        msgData.btnX = v.btnX
        msgData.btnY = v.btnY
        msgData.btnUrl = v.btnUrl
        msgData.desc = v.desc
        if ExcelMapConf.scene[v.mapID] then
            msgData.isPVP = ExcelMapConf.scene[v.mapID].isPVP
        else
            msgData.isPVP = 0
        end
        msgData.fns[0] = 0
        for _,fn in ipairs(v.fns) do
            msgData.fns[0] = msgData.fns[0] + 1
            local msgFn = msgData.fns[msgData.fns[0]]
            msgFn.icon = fn[1]
            msgFn.fnDesc = fn[2]
        end
    end
    Msg.send(msgRet,human.fd)
end


function CG_AI_DESC_QUERY(human, m)
end
